import requests
import re
import os
if __name__ == '__main__':
    if not os.path.exists('./qiutuPics'):
        os.mkdir('./qiutuPics')
    url = 'https://www.qiushibaike.com/imgrank/page/'
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
    }
    ex = '<div class="thumb">.*?<img src="(.*?)" .*?</div>'
    all_pic_src = []
    for page in range(1,5):
        pageurl = url + str(page)
        res = requests.get(pageurl,headers).text
        all_pic_src += re.findall(ex, res, re.S)
    for pic in all_pic_src:
        res_pic = requests.get(url='https:'+pic,headers=headers).content
        img_name = pic.split('/')[-1]
        path = './qiutuPics/'+img_name
        f = open(path,'wb')
        f.write(res_pic)
        print(f'{img_name}下载成功！')
    f.close()
    print('爬取完毕！')
